﻿<UserControl 
    x:Class="TelecoSystems.Modules.Customers.Views.CustomersBrowseView"
    xmlns:dataForm="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:help="clr-namespace:TelecoSystems.Infrastructure.Helpers;assembly=TelecoSystems.Infrastructure"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
    xmlns:ee="http://schemas.microsoft.com/expression/2010/effects" 
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input" 
    xmlns:System="clr-namespace:System;assembly=mscorlib"	
    xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" 
    xmlns:vm="clr-namespace:TelecoSystems.Modules.Customers.ViewModels"
    xmlns:cmd="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
    mc:Ignorable="d"
    d:DesignHeight="800" d:DesignWidth="1100">

    <UserControl.Resources>
        <ResourceDictionary>
            <help:ListboxMarginConverter x:Key="listboxMarginConverter"/>
            <help:EmailConverter x:Key="mailtoConverter"/>
            <help:GenderToImageConverter x:Key="genderToImageConverter"/>
            <help:GenderToBoolConverter x:Key="genderToBoolConverter"/>
            <help:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
            <vm:CustomersBrowseViewModel x:Key="ViewModel" x:Name="ViewModel"/>

            <DataTemplate x:Key="CustomersDataTemplate">
                <Grid Height="Auto" Margin="0,0,10,0" Width="Auto">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="16"/>
                        <RowDefinition Height="16"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.RowSpan="3" Grid.Column="0" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" Source="{Binding Gender, Mode=OneWay, Converter={StaticResource genderToImageConverter}}" Width="32" Height="38" />
                    <TextBlock Grid.Row="0" Grid.Column="1" FontSize="16" Text="{Binding Name, Mode=OneWay}" TextWrapping="Wrap" Margin="15,0,0,0" VerticalAlignment="Top"/>
                    <StackPanel Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="15,0,0,0" Orientation="Horizontal">
                        <TextBlock Text="{Binding AddressStreet, Mode=OneWay}" Margin="0,0,10,0"/>
                        <TextBlock Text="{Binding AddressCity, Mode=OneWay}" Margin="0,0,10,0"/>
                        <TextBlock Text="{Binding AddressCountry, Mode=OneWay}" Margin="0,0,10,0"/>
                        <TextBlock Text="{Binding AddressPostalCode, Mode=OneWay}" Margin="0,0,10,0"/>
                    </StackPanel>
                    <StackPanel Grid.Row="2" Grid.Column="1" Margin="15,0,0,0" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Orientation="Horizontal">                        
                        <TextBlock Text="Birth Date:"/>
                        <TextBlock Text="{Binding BirthDate, Mode=OneWay}"/>
                    </StackPanel>
                    <StackPanel Grid.Row="2" Grid.Column="1" Margin="15,0,0,0" Visibility="{Binding DataContext.IsLoadingCompanies, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Orientation="Horizontal">
                        <TextBlock Text="ID Number:"/>
                        <TextBlock Text="{Binding IDNumber, Mode=OneWay}"/>
                    </StackPanel>
                    <TextBlock Grid.Row="0" Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Top" Text="Contact" TextWrapping="Wrap"/>
                    <TextBlock Grid.Row="1" Grid.Column="3" HorizontalAlignment="Right" Text="{Binding Phone, Mode=OneWay}" TextWrapping="Wrap"/>
                    <HyperlinkButton Grid.Row="2" Grid.Column="3" Foreground="#FF333333" Content="{Binding Email, Mode=OneWay}" d:LayoutOverrides="Height" NavigateUri="{Binding Email, Converter={StaticResource mailtoConverter}, Mode=OneWay}" BorderThickness="0,0,0,1" BorderBrush="#FF3694C9" HorizontalAlignment="Left"/>
                </Grid>
            </DataTemplate>
            <help:DetailVisibilityConverter x:Key="detailConverter"/>

            <DataTemplate x:Key="CustomerAutoCompleteDataTemplate">
                <Grid Margin="5">
                    <TextBlock Margin="0" Text="{Binding Name, Mode=OneWay}" TextWrapping="Wrap" d:LayoutOverrides="Width, Height"/>
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="AutoEmailDataTemplate">
                <Grid Margin="5">
                    <TextBlock Margin="0" Text="{Binding Email, Mode=OneWay}" TextWrapping="Wrap" d:LayoutOverrides="Width, Height"/>
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>
    <controlsToolkit:BusyIndicator x:Name="ModuleBusyIndicator" BusyContent="{Binding BusyContent}" IsBusy="{Binding IsBusy}" Foreground="Black" DisplayAfter="0:0:0.0" FontSize="11" OverlayStyle="{StaticResource ActivityOverlayStyle}" RenderTransformOrigin="0.5,0.5">
        <Grid x:Name="LayoutRoot">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="36" />
                    <RowDefinition MinHeight="380" />
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Row="0" Grid.Column="0" Margin="10" Orientation="Horizontal">
                    <TextBlock Text="Customer type: " Foreground="#FFCCCCCC"/>
                    <RadioButton GroupName="CustomerType" Content="People" IsChecked="{Binding IsLoadingPeople, Mode=TwoWay}" Foreground="#FFD0D0D0" />
                    <RadioButton GroupName="CustomerType" Content="Companies" IsChecked="{Binding IsLoadingCompanies, Mode=TwoWay}" Foreground="#FFD0D0D0" />
                </StackPanel>
                <StackPanel Grid.Row="1" Grid.Column="0" Margin="0,8,0,0" Orientation="Horizontal">
                    <StackPanel Orientation="Horizontal" Visibility="{Binding IsLoadingPeople, Converter={StaticResource BoolToVisibilityConverter}}">
                        <TextBlock HorizontalAlignment="Left" Margin="10,4,0,0" VerticalAlignment="Top" Text="Name" Foreground="#FFEEEEEE"/>
                        <input:AutoCompleteBox Text="{Binding NameFilter, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="154" ItemsSource="{Binding CustomersForFilter, Mode=OneWay}" ValueMemberPath="Name" FilterMode="Contains" ItemTemplate="{StaticResource CustomerAutoCompleteDataTemplate}" />                        
                        <TextBlock HorizontalAlignment="Right" Margin="20,5,0,0" VerticalAlignment="Top" Text="E-mail" Foreground="#FFEEEEEE"/>
                        <input:AutoCompleteBox Text="{Binding EmailFilter, Mode=TwoWay}" HorizontalAlignment="Right" Margin="10,0,0,0" VerticalAlignment="Top" Width="154" ItemsSource="{Binding CustomersForFilter, Mode=OneWay}" ValueMemberPath="Email" FilterMode="Contains" ItemTemplate="{StaticResource AutoEmailDataTemplate}" />
                        
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Visibility="{Binding IsLoadingCompanies, Converter={StaticResource BoolToVisibilityConverter}}">
                        <TextBlock HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Text="Name" Foreground="#FFEEEEEE"/>
                        <input:AutoCompleteBox Text="{Binding NameFilter, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="154" ItemsSource="{Binding Customers, Mode=OneWay}" ItemTemplate="{StaticResource CustomerAutoCompleteDataTemplate}" ValueMemberPath="Name" FilterMode="Contains" />
                    </StackPanel>
                </StackPanel>
                <ListBox Grid.Row="2" Grid.Column="0" x:Name="CustomersListbox" ItemsSource="{Binding Customers, Mode=TwoWay}" ItemTemplate="{StaticResource CustomersDataTemplate}" SelectedItem="{Binding Path=CustomerDetail, Mode=TwoWay}" Margin="15,15,15,10" VerticalAlignment="Top">
                </ListBox>
                <StackPanel Grid.Row="0" Grid.Column="1" Margin="20,10" Orientation="Horizontal">
                    <Button Content="Add" Margin="5,0" Height="24" cmd:Click.Command="{Binding AddCustomerPersonCommand}" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" Width="60"/>
                    <Button Content="Add" Margin="5,0" Height="24" cmd:Click.Command="{Binding AddCustomerCompanyCommand}" Visibility="{Binding DataContext.IsLoadingCompanies, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" Width="60"/>
                    <Button Content="Delete" Margin="5,0" Height="24" cmd:Click.Command="{Binding DeleteCustomerCommand}" Width="80" />                    
                </StackPanel>
                <Border Grid.Row="1" Grid.RowSpan="2" CornerRadius="6" Grid.Column="1" Margin="20,10" Padding="10" Background="LightGray">
                    <Grid Opacity="1" x:Name="grid" Height="Auto" DataContext="{Binding CustomerDetail, Mode=TwoWay}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />                            
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>                        
                        <TextBlock Margin="0,5,0,0" Text="First name" Grid.Row="0" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBox Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Height="24" Margin="0,5,8,0" Grid.Column="1" Grid.Row="0" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBlock Margin="0,5,0,0" Text="Last name" TextWrapping="Wrap" Grid.Row="1" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBox Text="{Binding LastName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Height="24" Margin="0,5,8,0" Grid.Column="1" Grid.Row="1" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBlock Margin="0,5,0,0" Text="Name" TextWrapping="Wrap" Grid.Row="2" VerticalAlignment="Top"/>
                        <TextBox Text="{Binding Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" VerticalAlignment="Top" Height="24" Margin="0,5,8,0" Grid.Column="1" Grid.Row="2"/>
                        <TextBlock Grid.Row="2" Margin="0,33,8,0" Text="Gender" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <StackPanel Grid.Row="2" Grid.Column="1" Margin="0,33,8,0" Orientation="Horizontal" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}">
                            <RadioButton Margin="10,0,0,0" GroupName="Gender" IsChecked='{Binding Gender, Converter={StaticResource genderToBoolConverter}, ConverterParameter="M", Mode=TwoWay}' Foreground="Black" Content="Male" />
                            <RadioButton Margin="10,0,0,0" GroupName="Gender" IsChecked='{Binding Gender, Converter={StaticResource genderToBoolConverter}, ConverterParameter="F", Mode=TwoWay}' Foreground="Black" Content="Female" />
                        </StackPanel>
                        <TextBlock Text="ID Number" TextWrapping="Wrap" Margin="0,33,8,0" Grid.Row="2" VerticalAlignment="Top" Visibility="{Binding DataContext.IsLoadingCompanies, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBox Margin="0,33,8,0" Text="{Binding IDNumber, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" VerticalAlignment="Top" Grid.Column="1" Grid.Row="2" Visibility="{Binding DataContext.IsLoadingCompanies, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <!--<Path Fill="Black" Stretch="Fill" Stroke="Black" Height="1" Opacity="0.25" UseLayoutRounding="False" Data="M31,366 L289,366" Grid.ColumnSpan="2"/>-->
                        <TextBlock Margin="0,5,0,0" Grid.Row="3" Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}" >Birth Date</TextBlock>
                        <controls:DatePicker Grid.Column="1" Grid.Row="3" Height="24" Margin="0,5,8,0" SelectedDate="{Binding BirthDate, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"  Visibility="{Binding DataContext.IsLoadingPeople, ElementName=LayoutRoot, Converter={StaticResource BoolToVisibilityConverter}}"/>
                        <TextBlock Margin="0,5,0,0" Text="E-mail address" Grid.Column="0" Grid.Row="4"/>
                        <TextBox Text="{Binding Email, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Height="24" Margin="0,5,8,0" Grid.Column="1" Grid.Row="4"/>
                        <TextBlock Margin="0,5,0,0" Text="Phone" TextWrapping="Wrap" Grid.Column="0" Grid.Row="5"/>
                        <TextBox Text="{Binding Phone, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Height="24" Margin="0,5,8,0" Grid.Column="1" Grid.Row="5"/>
                        <TextBlock Margin="0,5,0,0" Grid.Row="6" Text="Street"/>
                        <TextBox Grid.Column="1" Grid.Row="6" Height="24" Margin="0,5,8,0" Text="{Binding AddressStreet, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
                        <TextBlock Margin="0,5,0,0" Grid.Row="7" Text="City"/>
                        <TextBox Grid.Column="1" Grid.Row="7" Height="24" Margin="0,5,8,0" Text="{Binding AddressCity, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
                        <TextBlock Margin="0,5,0,0" Text="Postal code" Grid.Row="8"/>
                        <TextBox Grid.Column="1" Grid.Row="8" Height="24" Margin="0,5,8,0" Text="{Binding AddressPostalCode, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
                        <TextBlock Margin="0,5,0,0" Grid.Row="9" Text="Country"/>
                        <ComboBox Height="20" Grid.Column="1" Grid.Row="9" Margin="0,5,8,0" SelectedValue="{Binding AddressCountry, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True, TargetNullValue=''}" DisplayMemberPath="Name" SelectedValuePath="Name" ItemsSource="{Binding Path=Countries, Mode=OneWay, Source={StaticResource ViewModel}}">
                            <!--<ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>-->
                        </ComboBox>
                        <Button Grid.Row="10" Grid.Column="1" Margin="10" Width="80" cmd:Click.Command="{Binding Path=DataContext.ShowLocationCommand, ElementName=LayoutRoot}" Content="Show on Map"/>                                                
                        <StackPanel Grid.Row="11" Grid.RowSpan="2" Orientation="Horizontal">
                            <Button Content="Submit" Height="24" Margin="5,0" cmd:Click.Command="{Binding Path=DataContext.SubmitChangesCommand, ElementName=LayoutRoot}" />
                            <Button Content="Cancel" Height="24" Margin="5,0" cmd:Click.Command="{Binding Path=DataContext.CancelCommand, ElementName=LayoutRoot}" />
                        </StackPanel>
                    </Grid>
                    <Border.Effect>
                        <DropShadowEffect />
                    </Border.Effect>
                </Border>
            </Grid>
        </Grid>
    </controlsToolkit:BusyIndicator>
</UserControl>
